import com.mochimedia.ui.*;

class com.mochimedia.ui.FormField extends Component {
    
    private var _type:String = "formfield";
    
    private var _textLabel:String;

    private var _selectable:Boolean = true;
    
    private var _tf:TextField;
    
    private var _highlight:MovieClip;
    
    private var _useHTMLtext:Boolean = false;
    public function get useHTMLtext():Boolean { return _useHTMLtext; }
    public function set useHTMLtext(value:Boolean):Void {
        _useHTMLtext = _tf.html = value;
    }
    
    public function get text ():String { return _tf.text; }
    public function set text (val:String):Void { _tf.htmlText = _tf.text = val; }
    
    private var _clear:Boolean = true;
    public function get clear():Boolean { return _clear; }
    public function set clear(value:Boolean):Void { _clear = value; }
    
    public function get changed ():Boolean { return (_tf.text != _textLabel); }
    public function get value ():String { return (_tf.text != _textLabel) ? _tf.text : ""; }
    public function set value (val:String):Void { 
        if (_useHTMLtext) _tf.htmlText = (val.length > 0) ? val : "";
        else _tf.text = (val.length > 0) ? val : "";
    } 
    
    public function set editable (value:Boolean):Void {
        if (value) _tf.type = "input";
        else _tf.type = "dynamic";
    }
   
    
    public var validate:Function;
    
    
    public function FormField (container:MovieClip, textLabel:String, width:Number, height:Number, selectable:Boolean, position:Position, style:Style) {
        
        init (container, textLabel, width, height, selectable, position, style);
        
        if (_container != null) create();
        
    }
    
    //
    //
    private function init (container:MovieClip, textLabel:String, width:Number, height:Number, selectable:Boolean, position:Position, style:Style):Void {
        
        super.init(container, position, style);
        
        _textLabel = textLabel;
        _width = width;
        _height = height;
        _selectable = (selectable != undefined) ? selectable : _selectable;
        
    }
    
    //
    //
    public function create ():Void {

        super.create();
        
        if (isNaN(_width)) _width = _parentCell.width - _position.margin_left - _position.margin_right;
        
        _tf = Create.inputText(_textLabel, "formTF", _mc, _width - 6, NaN, _style);
        _tf._x = 6;
        _tf._y = 2;
        _tf.tabIndex = id;
        
        if (isNaN(_height)) {
            _height = _tf._height + 4;
        } else {
            _tf._height = _height - 4;
            _tf._y = Math.floor((_height - _tf._height) * 0.5);
            if (_height > 30) {
                _tf.multiline = true;
                _tf.wordWrap = true;
            }
        }
        
        DrawingMethods.emptyRect(_mc, false, 0, 0, _width, _height, 2, ColorTools.getTintedColor(_style.borderColor, _style.backgroundColor, 0.7));
        DrawingMethods.rect(_mc, false, 2, 2, _width - 4, _height - 4, _style.backgroundColor);

        connectTextField(_tf);
        
        if (_selectable) {
        
            addEventListener(EVENT_FOCUS, this, onFocus);
            addEventListener(EVENT_BLUR, this, onBlur);
        
        } else {
            
            _tf.selectable = false;
            
        }
        
        _width = _mc._width;
        _height = _mc._height;
        
        _highlight = _mc.createEmptyMovieClip("_highlight", 1);
        DrawingMethods.emptyRect(_highlight, false, 0, 0, _width, _height, 2, _style.borderColor);
        DrawingMethods.emptyRect(_highlight, false, -2, -2, _width + 4, _height + 4, 2, ColorTools.getSaturatedColor(_style.haloColor, 255), 60);
        _highlight._visible = false;

    }
	
	//
	//
	public function focus ():Void {
		
		Selection.setFocus(_tf);
		onFocus();
		if (_tf.text.length > 0) Selection.setSelection(0, _tf.text.length);
		
	}
    
    //
    //
    public function onFocus (e:Event):Void {
        
        if (_enabled) {
            
            _highlight._visible = true;
            
            if (_tf.text == _textLabel && _tf.text.indexOf("...") != -1 && _clear) {
                
                _tf.text = _tf.htmlText = "";
                
            } else if (_selectable) {
            
                var self:Object = this;
                
                _highlight.onMouseUp = function ():Void {
                    
                    this.onEnterFrame = function ():Void {
                        
                        if (Selection.getBeginIndex() == Selection.getEndIndex()) {
                            
                            Selection.setFocus(self["_tf"]._target);
                            Selection.setSelection(0, self["_tf"].text.length); 
                            
                        }
                        
                        delete this.onEnterFrame;
                        
                    }
                    
                    delete this.onMouseUp;
                    
                }
            
            }
            
        }
    
    }
    
    //
    //
    public function onBlur (e:Event):Void {
        
        _highlight._visible = false;
        delete _highlight.onMouseUp;
        
        if (_tf.text == "" && _clear) {
            _tf.text = _textLabel;
            dispatchEvent(EVENT_CHANGE);
        }
        
    }
    
    //
    //
    public function enable ():Void {
        
        super.enable();
        
        _tf.textColor = _style.textColor;
        _tf.selectable = true;
        _tf.tabEnabled = true;
        
    }
    
    //
    //
    public function disable ():Void {
        
        super.disable();
        
        _tf.textColor = ColorTools.getTintedColor(_style.textColor, _style.backgroundColor, 0.8);
        _tf.selectable = false;
        _tf.tabEnabled = false;
        _highlight._visible = false;
        
    }
    
    //
    //
    public function lock ():Void {
        
        disable();
        
        _tf.textColor = ColorTools.getTintedColor(_style.textColor, _style.backgroundColor, 0.4);
        
    }
    
}